package com.app.Adapter;

import java.util.ArrayList;

import com.app.Activity.frmDanhSachRule;
import com.app.PhanQuyen.Quyen;
import com.app.RowDTO.RuleRowDTO;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import app.com.ModuleRole.R;

public class RuleAdapter extends BaseAdapter {
	public static class ViewHolder {
		TextView txtTenRule;
		CheckBox chkChon;
	}

	private Activity activity;
	private LayoutInflater inflater;
	private ArrayList<RuleRowDTO> data;

	public RuleAdapter(Activity activity, ArrayList<RuleRowDTO> data) {
		this.activity = activity;
		this.data = data;
		this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {

		return data.size();
	}

	@Override
	public Object getItem(int position) {

		return data.get(position);
	}

	@Override
	public long getItemId(int position) {

		return Long.parseLong(data.get(position).rule.getID());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = convertView;
		if (view == null) {
			view = inflater.inflate(R.layout.listrow_rule, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.chkChon = (CheckBox) view.findViewById(R.id.chkChon);
			view.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) view.getTag();
		holder.chkChon.setText(data.get(position).rule.getName());
		final int index = position;
		holder.chkChon.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				data.get(index).check = isChecked;
			}
		});
		holder.chkChon.setChecked(data.get(index).check);
		Log.d("phu", "getView:" + position + holder.chkChon.isChecked() + "-" + data.get(position).check);
		return view;
	}

	public void countSelectItem() {
		int n = data.size();
		int count = 0;
		for (int i = 0; i < n; i++) {
			if (data.get(i).check)
				count++;
		}

	}
}
